﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Economo.UI.Views;
using Economo.UI.AccountsServiceReference;
using Economo.Services.BusinessEntities;

namespace Economo.UI.Presenters
{
    public class ManageBanksPresenter : ManageEntitiesPresenter
    {
        public ManageBanksPresenter(IManageBanksView view) : base(view)
        {

        }
        private IManageBanksView BankView
        {
            get
            {
                return (IManageBanksView)View;
            }
        }
        #region Event Methods
        #endregion
        #region CRUD Methods
        protected override void UpdateEntity(int rowIndex)
        {
            AccountsServiceClient client = new AccountsServiceClient();
            try
            {
                Economo.Services.BusinessEntities.Bank updatedBank = GetBankFromGridRow(rowIndex);

                client.UpdateBank(new UpdateBankRequest(updatedBank));
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                client.Close();
            }
        }
        protected override void DeleteEntity(object id)
        {
            AccountsServiceClient client = new AccountsServiceClient();
            try
            {
                byte bankId = Convert.ToByte(id);
                client.DeleteBankById(new DeleteBankByIdRequest(bankId));
                List<Bank> banks = (List<Bank>)View.EntitiesList;
                Bank bank = (from b in banks
                             where (b.Id == bankId)
                             select b).FirstOrDefault<Bank>();
                banks.Remove(bank);
                View.EntitiesList = banks;
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                client.Close();
            }
        }
        protected override void AddNewEntity()
        {
            List<Bank> banks = (List<Bank>)View.EntitiesList;
            AccountsServiceClient client = new AccountsServiceClient();
            try
            {
                Bank newBank = new Bank();
                newBank.Description = "(replace)";
                newBank.Id = client.InsertBank(new InsertBankRequest(newBank)).BankId;
                banks.Add(newBank);

                View.EntitiesList = banks;
            }
            finally
            {
                client.Close();
            }
        }

        #endregion
        #region Side Methods
        private Economo.Services.BusinessEntities.Bank GetBankFromGridRow(Int32 rowIndex)
        {
            Economo.Services.BusinessEntities.Bank bankOnGridRow = new Economo.Services.BusinessEntities.Bank();
            bankOnGridRow.Id = Convert.ToByte(View.GetEntityIdFromGridRow(rowIndex));
            bankOnGridRow.Description = BankView.GetBankDescriptionFromGridRow(rowIndex);
            bankOnGridRow.Picture = BankView.GetBankPictureFromGridRow(rowIndex);
            return bankOnGridRow;
        }
        #endregion

        #region FillMethods

        public override void FillData()
        {
            AccountsServiceClient client = new AccountsServiceClient();
            try
            {
                
                List<Bank> banks = client.GetBanksByDescription(new GetBanksByDescriptionRequest("")).Banks;
                View.EntitiesList = banks;
            }
            catch
            {

            }
            finally
            {
                client.Close();
            }
        }

        #endregion
    }
    
}
